package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import com.itheima.utils.MyTransactionManager;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.SQLException;

@Repository
public class UserDaoImpl implements UserDao{
    @Autowired
    private QueryRunner queryRunner;

    @Autowired
    private MyTransactionManager myTransactionManager;

    @Override
    public void out(String outName, Integer money) throws SQLException {
        System.out.println("转出开始执行了");
        String sql = "update tal_account set money = money - ? where username = ?";
        Object[] params = {money,outName};
        int count = queryRunner.update(myTransactionManager.getConnection(),sql,params);
        System.out.println(count);
        //connection.close();
        //此处的dao千万不要释放资源  因为一单connection对象关闭了  那么下面的转入无法进行查询 因为连接关闭了
        //java.sql.SQLException: connection holder is null Query:  持有连接的是一个空的查询
    }

    @Override
    public void in(String inName, Integer money) throws SQLException {
        System.out.println("转入开始执行了");
        String sql = "update tal_account set money = money + ? where username = ?";
        Object[] params = {money,inName};
        int count = queryRunner.update(myTransactionManager.getConnection(),sql,params);
        System.out.println(count);
    }
}
